//
//  PingInvertTransition.m
//  KYPingTransition
//
//  Created by koki on 15/11/10.
//  Copyright © 2015年 apple. All rights reserved.
//

#import "PingInvertTransition.h"

@interface PingInvertTransition()

@property(nonatomic,strong)id<UIViewControllerContextTransitioning>transitionContext;

@end

@implementation PingInvertTransition

-(CGRect)endRect
{
    if (_endRect.size.width==0 && _endRect.size.height==0)
    {
        _endRect=CGRectMake(0, 0, 10, 10);
    }
    return _endRect;
}

- (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext{
    return 0.7f;
}

- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext{
    
    
    self.transitionContext = transitionContext;
    
    UIViewController *fromVC = (UIViewController *)[transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIViewController *toVC   = (UIViewController *)[transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    UIView *containerView = [transitionContext containerView];
    
    
    [containerView addSubview:toVC.view];
    [containerView addSubview:fromVC.view];

    
    UIBezierPath *finalPath = [UIBezierPath bezierPathWithOvalInRect:self.endRect];
    
    CGPoint finalPoint;
    CGPoint centerPoint=CGPointMake(self.endRect.origin.x+self.endRect.size.width/2.0, self.endRect.origin.y+self.endRect.size.height/2.0);
    
    if(self.endRect.origin.x > (toVC.view.bounds.size.width / 2)){
        if (self.endRect.origin.y < (toVC.view.bounds.size.height / 2)) {
            finalPoint = CGPointMake(centerPoint.x - 0, centerPoint.y - CGRectGetMaxY(toVC.view.bounds)+30);
        }else{
            finalPoint = CGPointMake(centerPoint.x - 0,centerPoint.y - 0);
        }
    }else{
        if (self.endRect.origin.y < (toVC.view.bounds.size.height / 2)) {
            finalPoint = CGPointMake(centerPoint.x - CGRectGetMaxX(toVC.view.bounds), centerPoint.y - CGRectGetMaxY(toVC.view.bounds)+30);
        }else{
            finalPoint = CGPointMake(centerPoint.x - CGRectGetMaxX(toVC.view.bounds), centerPoint.y - 0);
        }
    }

    

    CGFloat radius = sqrt(finalPoint.x * finalPoint.x + finalPoint.y * finalPoint.y);
    UIBezierPath *startPath = [UIBezierPath bezierPathWithOvalInRect:CGRectInset(self.endRect, -radius, -radius)];
    
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.path = finalPath.CGPath;
    fromVC.view.layer.mask = maskLayer;
    
    
    CABasicAnimation *pingAnimation = [CABasicAnimation animationWithKeyPath:@"path"];
    pingAnimation.fromValue = (__bridge id)(startPath.CGPath);
    pingAnimation.toValue   = (__bridge id)(finalPath.CGPath);
    pingAnimation.duration = [self transitionDuration:transitionContext];
    pingAnimation.timingFunction = [CAMediaTimingFunction  functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

    pingAnimation.delegate = self;
    
    [maskLayer addAnimation:pingAnimation forKey:@"pingInvert"];
    
}


- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
    
    [self.transitionContext completeTransition:![self.transitionContext transitionWasCancelled]];
    [self.transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey].view.layer.mask = nil;
    [self.transitionContext viewControllerForKey:UITransitionContextToViewControllerKey].view.layer.mask = nil;

}


@end





